package com.happe.foodeasy.winter.conf.parser;

import com.happe.foodeasy.winter.typemap.ParseTypeMap;

import java.lang.reflect.Field;

/**
 * This class is used to store all type map between java type and target database type.
 * Created by wubin on 2016-05-19.
 */
public class DatabaseTypeMapParseResult {
    private String numberType;
    private String byteArrayType;
    private String stringType;
    private String booleanType;
    private String dateType;

    private ParseTypeMap parseTypeMap;

    private DatabaseTypeMapParseResult(Builder builder) {
        this.numberType = builder.numberType;
        this.byteArrayType = builder.byteArrayType;
        this.stringType = builder.stringType;
        this.booleanType = builder.booleanType;
        this.dateType = builder.dateType;
    }

    public String getNumberType(int decimalDigit) {
        return String.format(numberType, "*", decimalDigit);
    }

    public String getNumberType(int digitUpperLimit, int decimalDigit) {
        return String.format(numberType, digitUpperLimit, decimalDigit);
    }

    public String getByteArrayType() {
        return byteArrayType;
    }

    public String getStringType(int stringLength) {
        return String.format(stringType, stringLength);
    }

    public String getBooleanType() {
        return booleanType;
    }

    public String getDateType() {
        return dateType;
    }

    public String parseDatabaseTypeByField(Field field) {
        return parseTypeMap.startTypeMap(field, this);
    }


    public void setParseTypeMap(ParseTypeMap parseTypeMap) {
        this.parseTypeMap = parseTypeMap;
    }

    @SuppressWarnings("all")
    public static class Builder {
        private String numberType;
        private String byteArrayType;
        private String stringType;
        private String booleanType;
        private String dateType;

        public void setNumberType(String numberType) {
            this.numberType = numberType;
        }

        public void setByteArrayType(String byteArrayType) {
            this.byteArrayType = byteArrayType;
        }

        public void setStringType(String stringType) {
            this.stringType = stringType;
        }

        public void setBooleanType(String booleanType) {
            this.booleanType = booleanType;
        }

        public void setDateType(String dateType) {
            this.dateType = dateType;
        }

        public DatabaseTypeMapParseResult build() {
            return new DatabaseTypeMapParseResult(this);
        }
    }
}
